﻿using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection;

namespace Dapper.ExpressionExtension
{
    /// <summary>
    /// 列属性
    /// </summary>
    public class ColumnPropertyInfo
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="property"></param>
        /// <param name="isKey"></param>
        public ColumnPropertyInfo(PropertyInfo property, bool isKey = false)
        {
            Property = property ?? throw new ArgumentNullException(nameof(property));
            GeneratedOption = property.GetCustomAttribute<DatabaseGeneratedAttribute>()?.DatabaseGeneratedOption
                ?? (isKey ? DatabaseGeneratedOption.Identity : DatabaseGeneratedOption.None);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="property"></param>
        /// <param name="generatedOption"></param>
        public ColumnPropertyInfo(PropertyInfo property, DatabaseGeneratedOption generatedOption)
        {
            Property = property ?? throw new ArgumentNullException(nameof(property));
            GeneratedOption = generatedOption;
        }

        /// <summary>
        /// 
        /// </summary>
        public PropertyInfo Property { get; }

        /// <summary>
        /// 
        /// </summary>
        public DatabaseGeneratedOption GeneratedOption { get; }

        /// <summary>
        /// 
        /// </summary>
        public bool IsGenerated => GeneratedOption != DatabaseGeneratedOption.None;
    }
}
